{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3b138471-6d7c-4056-81aa-a07a8e31359b",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Robodyno Slider API"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0eebd467-dd6d-4a46-87c4-dcba1ecb2aa7",
   "metadata": {},
   "source": [
    "## 1.初始化Slider对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7d45e69-f8ca-4f69-8f16-1d3e8498fd69",
   "metadata": {},
   "source": [
    "`SliderModule(can, id_ = 0x10, type_ = None, max_vel = 0.02, lead = 0.01)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `can` : robodyno 接口对象\n",
    "- `id_` : 电机id，默认 : 0x10\n",
    "- `type_` : 电机类型\n",
    "- `max_vel` : 最大速度，默认 : 0.02m/s\n",
    "- `lead` : 直线电机导程，默认 : 0.01m/round"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3ce3d8d0-e41a-449a-b073-5f6fff647abc",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from robodyno.interfaces import CanBus\n",
    "from robodyno.components import SliderModule\n",
    "can = CanBus()\n",
    "slider = SliderModule(can, 0x10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58ad11d7-deb1-4fc4-84d6-d098863e9624",
   "metadata": {},
   "source": [
    "## 2.设置最大速度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40b094b1-da3d-4c63-afd0-41f1bb46c21d",
   "metadata": {},
   "source": [
    "`set_max_vel(max_vel)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `max_vel`: 直线模块的最大速度，其值应为正数(m/s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8f9a309d-6059-48e7-9353-732eac49f58a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "slider.set_max_vel(0.02)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d2e4d96-4b77-4204-8fe4-f1c97c506491",
   "metadata": {},
   "source": [
    "## 3.直线滑块使能"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2c13ef6-b283-46f3-bdd4-f2763a4b854c",
   "metadata": {},
   "source": [
    "`enable()`\n",
    "\n",
    "- 使能后直线滑块将处于上电状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a05b698e-ed63-4967-bd52-696d15dd1a2f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "slider.enable()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ca6e403-a6b5-4500-9cad-ab92b21a9e9f",
   "metadata": {},
   "source": [
    "## 4.直线滑块失能"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e098fe04-1372-40f6-8f54-d6ece9b00dc2",
   "metadata": {},
   "source": [
    "`disable()`\n",
    "\n",
    "- 失能后直线滑块将处于掉电状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3f15f180-7ff7-4d1b-b26f-3a81371f0211",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "slider.disable()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5da7c98-0a7f-4f35-b1e4-d5bf08beab7b",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 5.初始化当前位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1ecfc08-0624-4e7e-974f-f21aea4d8296",
   "metadata": {},
   "source": [
    "`init_pos(offset = 0)`\n",
    "\n",
    "参数:\n",
    "\n",
    "- `offset`: 当前位置(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "623aa154-c939-44fb-b615-c387caa4c134",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "slider.init_pos(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2b796e7-a474-4d08-adbe-6fefee92763d",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 6.初始化绝对位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "add669ca-91bc-442f-9078-3e3197e741f1",
   "metadata": {},
   "source": [
    "`init_abs_pos(offset = 0, save = True)`\n",
    "\n",
    "参数:\n",
    "- `offset` : 当前绝对位置(m)\n",
    "- `save` : 是否保存当前绝对位置\n",
    "    - `True` : 保存当前绝对位置\n",
    "    - `False` : 不保存当前绝对位置\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "b1721d6c-a02f-4e66-b6c5-918263a5f749",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "slider.init_abs_pos(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f259b00d-809b-4dab-8a57-fd8aa3dc0a4d",
   "metadata": {},
   "source": [
    "## 7.设置位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77832044-8b52-4ad7-9aab-b2ea23201644",
   "metadata": {},
   "source": [
    "`set_pos(pos)`\n",
    "\n",
    "参数:\n",
    "\n",
    "- `pos`: 目标位置(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e18cf074-ef41-4c76-8909-e106877c15fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "slider.set_pos(0.05)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e40d6679-a9e4-4e07-9ed5-e27d4dfe3732",
   "metadata": {},
   "source": [
    "## 8.设置绝对位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39eb9fc2-8cf5-4e12-9af4-f90429fc1627",
   "metadata": {},
   "source": [
    "`set_abs_pos(pos)`\n",
    "\n",
    "参数:\n",
    "\n",
    "- `pos`: 目标绝对位置(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8497d812-2150-4082-96e3-b8b6a1fb361d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "slider.set_abs_pos(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf813068-bf69-4b1c-a048-25e5e1e7a195",
   "metadata": {},
   "source": [
    "## 9.读取直线滑块位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da70e3d6-be11-486a-8b7f-845b5edbd0c9",
   "metadata": {},
   "source": [
    "`get_pos(timeout = 0)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，0代表无超时时间\n",
    "\n",
    "返回值：\n",
    "\n",
    "- 位置(m)，超时则不返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "27cc3d33-87c8-49b0-85b1-6ebd289358e5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.6226043701171875e-08"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "slider.get_pos(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de029338-c0cf-4331-8661-c7f9ef3168fd",
   "metadata": {},
   "source": [
    "## 10.读取直线滑块绝对位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57b3d4e9-2aba-44ae-bddc-6145eb9a21dd",
   "metadata": {},
   "source": [
    "`get_abs_pos(timeout = 0)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)，0代表无超时时间\n",
    "\n",
    "返回值：\n",
    "\n",
    "- 绝对位置(m)，断电不丢失，超时则不返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "af8e20b3-454d-4374-bafb-46683ba3a4b7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.003913734175942161"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "slider.get_abs_pos(1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
